package com.leaguefox.domain;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Version;

import org.springframework.beans.factory.annotation.Configurable;


@Configurable
@Entity
public class Team {
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "team_id")
	private Long teamId;

	@Column(name = "name")
	private String name;

	@ManyToMany(fetch = FetchType.EAGER)
	@JoinTable(name = "TEAM_PLAYERS", joinColumns = @JoinColumn(name = "TEAM_ID", referencedColumnName = "TEAM_ID"), inverseJoinColumns = @JoinColumn(name = "PLAYER_ID", referencedColumnName = "PLAYER_ID"))
	private List<Player> players = new ArrayList<Player>();

	@Column(name = "SINGLE_PLAYER_ONLY")
	private boolean singlePlayerOnly;

	@Version
	@Column(name = "version")
	private Integer version;

	public void addPlayer(Player player) {
		this.players.add(player);
	}

	public Long getId() {
		return this.teamId;
	}

	public String getName() {
		return name;
	}

	public List<Player> getPlayers() {
		return new ArrayList<Player>(this.players);
	}

	public Integer getVersion() {
		return this.version;
	}

	public boolean isSinglePlayerOnly() {
		return singlePlayerOnly;
	}

	public void removePlayer(Player player) {
		this.players.remove(player);
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setPlayers(List<Player> players) {
		this.players.addAll(players);
	}

	public void setSinglePlayerOnly(boolean singlePlayerOnly) {
		this.singlePlayerOnly = singlePlayerOnly;
	}

	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("Id: ").append(getId()).append(", ");
		sb.append("Version: ").append(getVersion()).append(", ");
		sb.append("Players: ").append(
				getPlayers() == null ? "null" : getPlayers().size());
		return sb.toString();
	}

}
